<?php
/**
 * ExcelTemplateService.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-04-07 23:59:00
 * @modified    2023-04-07 23:59:00
 *
 */

namespace App\Services;

class ExcelTemplateService
{
    private $basePath;
    private $baseUrl;

    public function __construct()
    {
        $this->baseUrl = env('APP_URL') . '/resources/template/';
        $this->basePath = public_path('resources/template/');
    }

    public function customer()
    {
        $fileName = 'template-customer.xlsx';
        return $this->getFileUrl($fileName);
    }

    public function product()
    {
        $fileName = 'template-product.xlsx';
        return $this->getFileUrl($fileName);
    }

    public function project_product()
    {
        $fileName = 'template-project-product.xlsx';
        return $this->getFileUrl($fileName);
    }

    private function getFileUrl($fileName)
    {
        $basePath = $this->basePath;
        $filePath = $basePath . $fileName;
        if (!is_file($filePath)) {
            throw new \Exception('模板文件不存在');
        }
        $baseUrl = $this->baseUrl;
        return $baseUrl . $fileName;
    }
}